var user = liwe.module ( "user", { "change_password": "_change_password" } );

user._uid = -1;

user._profile_dest_div = "user_profile";

user._panel_id = null;
user._panel_label =  "Dati Utente";
user._panel_render_menu = true;
user.password_change_div="main_content";

user.init = function ( cback )
{
	user.valid_events ( [ 'login', 'is-logged', 'logout', 'register_check', 'register', 'lostpwd', 'rendered', 'profile', 'modify', 'stat-login', 'stat-logout', 'add_box', 
		'user-data-multiset', 'data-type-get', 'data-type-list', 'user-data-get', 'password_change_form', 'password_change' ] ); 
	user.load_templates ( cback );

	user._profile_list = list ();

	user.addEventListener ( 'stat-login', function ( e ) 
	{
		var p = e.params, txt = "UID: %(uid)s - Extra: %(extra)s";
		liwe.ga.event ( 'User', 'login', String.formatDict ( txt, p ) );
	}, true ); 

	user.addEventListener ( 'stat-logout', function ( e )  
	{
		liwe.ga.event ( 'User', 'logout', e.params.uid );
	}, true ); 

	user.addEventListener ( 'user-data-multiset', function ( e )
	{
		//$( 'profile_cnt' , String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], resp ) );	
		$( 'profile_cnt' , user.profile_content ( resp ) ); 

		//user.dispatchEvent ( "modify", null, resp );
		
	} );

	user.addEventListener ( 'profile' , function ( e )
	{
		var data = e.params;
		//var content = String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], data );
		var content = user.profile_content ( data ); 
		user.profile_add_box ( content, "user", user._panel_id, user._panel_label, data );
	}, true );

	user.addEventListener ( 'add_box' , function ( e )
	{
		var data = e.params;
		if ( data.id_panel == data.module + '_profile' )
		{
			var dest_div =  $el ( "profile_ws" );
			dest_div.insertAdjacentHTML ( "beforeEnd", String.formatDict ( user.templates [ "SITE_PROFILE_BOX_PANEL" ] , data ) );
		} else {
			$( data.id_panel, String.formatDict ( user.templates [ "SITE_PROFILE_BOX_PANEL" ] , data ) );
		};
		if ( ! user._panel_render_menu ) return;
				
		var dest_div =  $el ( "profile_menu" );
		dest_div.insertAdjacentHTML ( "beforeEnd", String.formatDict ( user.templates [ "SITE_PROFILE_BOX_MENU" ] , data ) );
		if ( data.module == "user" ) user._panel_id = data.id_panel;
		
	}, true );

	user.addEventListener ( 'password_change_form', function ( e )
	{
		var f = e.params [ 'form_obj' ];
		
		f.set ( user.password_change_div, user.templates [ 'SITE_PASSWORD_CHANGE_FORM' ] ) ;
	}, true );

	user.addEventListener ( 'password_change', function ( e )
	{
		var data = e.params;
		alert ( data [ 'message' ] );

	}, true );
};
user.logout = function ()
{
	var get =  { 'action' : 'user.ajax.get_info' };
	user.ajax ( get , function ( res )
	{
		
	} );
	user.ajax ( { action: "user.ajax.logout" }, function () 
	{

		user.dispatchEvent ( "stat-logout", null, { 'uid' : user._uid } );
		user._uid = -1; 
		//new event dispatch
		user.dispatchEvent ( "logout", null, { 'res' : 'ok' } );

	} );
};
//{{{USER WITH FORM 5
//{{{user.render_login
user.render_login = function ( dest_div, show_lost_pwd, module, registration_form )
{
	if ( typeof ( module ) == 'undefined' ) module = 'user';

	user.ajax ( { action : 'user.ajax.get_info' }, function ( res )
	{
		res [ 'module' ] = module;

		user._uid = res.get ( "uid", -1 );

		if ( user._uid != -1 )
		{
			user._uid = res [ 'uid' ];
			// CHECK: e' necessario fare il dispatch degli eventi qui?
			//        se si chiama piu' volte la render_login e' sbagliato ...
			user.dispatchEvent ( "stat-login", null, res );
			user.dispatchEvent ( "is-logged", null, res );
		} else {
			$( dest_div, user.templates [ 'frm-user-layout' ] );
			// $( dest_div, user.templates [ 'SITE_LOGIN_BOX' ] );
			// user._make_login_form ( show_lost_pwd, module, registration_form );
			// user._make_registration_form  ( module );
			
			user._module = module;
			user.show_form ( 'login' );
			user.dispatchEvent ( "rendered", null, res );
		}
	} );
};
//}}}
//{{{user.show_form
user.show_form = function ( type )
{
	var tb = $( 'user-layout-toolbar' );

	liwe.dom.del_class ( $( 'user-btn-login' ), 'active' );
	liwe.dom.del_class ( $( 'user-btn-registration' ), 'active' );
	liwe.dom.del_class ( $( 'user-btn-lost-pwd' ), 'active' );

	liwe.dom.add_class ( $( 'user-btn-' + type ), 'active' );

	switch ( type )
	{
		case 'login':
			user._show_login_form ();
			break;
		case 'registration':
			user._show_registration_form ();
			break;
		case 'lost-pwd':
			user._show_lost_pwd_form ();
			break;
	}
};
//}}}
//{{{user._show_login_form
user._show_login_form = function ()
{
	var frm = new liwe.form5.instance ( "user-login-form" );

	frm.action = "user.ajax.login";
	frm.add ( 'hidden',   'module',   { value: user._module } );
	frm.add ( 'text',     'login',    { label: "Login / E-Mail", maxlength: 50, mandatory: true, placeholder: "yourname@example.com" } );
	frm.add ( 'password', 'pwd',      { label: "Password", maxlength: 30, mandatory: true, placeholder: "password" } );
	frm.add ( 'submit',   'do_login', { value: "Accedi" } );

	frm.set ( "user-layout-content", user.templates [ "frm-login" ] );

	frm.addEventListener ( "complete", function ( e )
	{
		var res = user.dispatchEvent ( "login", null, e.params.form.get_values() );

		if ( typeof ( res ) == 'undefined' ) res = true;

		return res;
	} );
};
//}}}
//{{{user._show_registration_form
user._show_registration_form = function ()
{
	var frm = new liwe.form5.instance ( "user-registration-form" );

	frm.action = "user.ajax.register";
	frm.add ( 'hidden', 'module', { value: user._module } );
	frm.add ( 'email',  'email',  { label: "Indirizzo e-mail", maxlength: 200, mandatory: true, placeholder: "yourname@example.com" } );
	frm.add ( 'password', 'pwd',  { label: "Password", maxlength: 30, mandatory: true, placeholder: "password" } );
	frm.add ( 'password', 'pwd2',  { label: "Password", maxlength: 30, mandatory: true, placeholder: "conferma password" } );
	frm.add ( 'submit',   'reg',   { value: "Registrati" } );

	frm.set ( "user-layout-content", user.templates [ 'frm-registration' ] );

	frm.addEventListener ( "complete", function ( e )
	{
		var res = user.dispatchEvent ( "register", null, e.params.form.get_values() );

		if ( typeof ( res ) == 'undefined' ) res = true;

		return res;
	} );
}
//}}}
//{{{user._show_lost_pwd_form
user._show_lost_pwd_form = function ()
{
	var frm = new liwe.form5.instance ( "user-lost-pwd-form" );

	frm.action = "user.ajax.lost_password";
	frm.add ( 'email',  'email',  { label: "Indirizzo e-mail", maxlength: 200, mandatory: true, placeholder: "yourname@example.com" } );
	frm.add ( 'submit', 'send',   { value: "Invia" } );

	frm.set ( "user-layout-content", user.templates [ 'frm-lost-pwd' ] );

	frm.addEventListener ( "submit", function ( e )
	{
		var res = user.dispatchEvent ( "lostpwd", null, e.params );

		if ( typeof ( res ) == 'undefined' ) res = true;

		return res;
	} );
}
//}}}

//{{{user._change_password
user._change_password = function ( data )
{
	if ( ! data || typeof ( data ) != 'object' || ! data [ 'token' ] ) return;

	var f = user.change_password_form_get ( data [ 'email' ], data [ 'token' ] );

	user.dispatchEvent ( "password_change_form", null, { 'form_obj' : f } );

};
//}}}
//{{{user.change_password_form_get
user.change_password_form_get = function ( email, token )
{
	var frm = new liwe.form5.instance ( "user-change-pwd-form" );
	
	frm.add ( 'password', 'pwd',  { label: "Password", maxlength: 30, mandatory: true, placeholder: "password" } );
	frm.add ( 'password', 'pwd2',  { label: "Verifica Password", maxlength: 30, mandatory: true, placeholder: "conferma password" } );
	frm.add ( 'hidden', 'email', { value: email } );
	frm.add ( 'hidden', 'token', { value: token } );
	frm.addEventListener ( "submit", user.change_password );
	return frm
};
//}}}
//{{{user.change_password
user.change_password = function ( evt )
{
	var sendevent = function ( valid , message )
	{
		user.dispatchEvent ( "password_change", null, { "valid" : valid, "message" : message } );
	}

	var f = evt.params.form

	if ( ! f.check () ) return;

	a = f.get_values ();

	if ( a [ 'pwd' ] != a [ 'pwd2' ] ) 
	{
		sendevent ( false, "Le due password non coincidono" )
		return false;
	}

	user.ajax ( { action: "user.ajax.change_password", pwd: a [ 'pwd' ], token: a [ 'token' ], email : a [ 'email' ] }, function ( v )
		{
			sendevent ( true, "Password Aggiornata!" )
		} );
	return false

};
//}}}
//}}}
//{{{user.render_profile = function ( dest_div, dct )
user.render_profile = function ( dest_div, dct )
{
	var render = function ( data )
	{
		
		user.load_templates ( function ()
		{
			user.__info	 = data;
			$( dest_div , user.templates [ 'SITE_PROFILE_BOX' ] );	
			user.dispatchEvent ( "profile", null, data );
		} );
	};

	if ( typeof ( dct ) == 'undefined' )
	{
		var post =  { 'action' : 'user.ajax.get_full_info' };
		user.ajax ( post, function ( res )
		{
			if ( res.get ( 'uid' , -1 ) != -1 )
				return render ( res );
		} );
	} else {
		return render ( dct );
	}
};
//}}}
//{{{user.profile_add_box = function ( html, module )
user.profile_add_box = function ( content, module, id_panel, label , data )
{
	if ( ! id_panel ) var id_panel = module + '_profile';
	this._profile_list.push ( [ label, id_panel ] );
	user.dispatchEvent ( "add_box", null,  { content : content, module : module, label : label, id_panel : id_panel, data : data } );
};
//}}}
user.profile_show_panel = function ( active_id )
{
	//REmove all active panel
	var panels = document.querySelectorAll ( ".user_info_panel" );

	for ( i = 0 ; i  < panels.length; i++	)
	{
		var p =  panels [ i ];
		liwe.dom.del_class ( p, 'active' );
	};
	liwe.dom.add_class ( $( active_id ) , 'active' ) ;
}

user.profile_content = function ( data )
{
	data = data || dict ();
	var user_data = new String.buffer ();
	data.get ( 'user_data', list () ).iterate ( function ( item )
	{
		user_data.add ( String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT_EXTRA' ], item ) );
	} );
	data [ 'extra_fields' ] = user_data.toString ();
	return ( String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], data ) );
};


//{{{user.modify_profile_form = function ()
user.modify_profile_form = function ()
{
	// FORM 5 and extra fields support
	$( 'profile_info' , user.templates [ 'SITE_PROFILE_BOX_MODIFY' ] );
	var data = user.__info || dict ();// set user data here
	var dest_div = 'modify_form';
	var f = new liwe.form5.instance ( "user-profile-form" );
	
	f.template = user.templates [ 'frm-user-modify-start' ];
	f.add ( "hidden", "dest_div", { "value" : dest_div } );
	f.add ( "input", "login", { "value" : data.get ( "login", "" ), "label" : "User", "mandatory" : true } );
	f.add ( "input", "email", { "value" : data.get ( "email", "" ), "label" : "Email", "mandatory" : true } );
	f.add ( "password", "oldpwd", { "label": "Password precedente", "mandatory" : true } );
	f.add ( "password", "pwd", { "label": "Password" } );
	f.add ( "password", "pwd2", { "label": "Password ( conferma )" } );
	f.template += user.add_data_fields ( f, data.get ( 'user_data', list () ), data ); //, user_data, data )
//	f.add ( "button", "btn_save", { "class" : "green", "value": "Aggiorna", "onclick": "user.modify();" , "nonl" : true } );
//	f.add ( "button", "cancel", { "value": "Annulla", "class" : "grey", "onclick" : "user.modify(true);" } );
	f.addEventListener ( "submit", user.modify );
	f.addEventListener ( "reset", user.modify_exit );
	f.template += user.templates [ 'frm-user-modify-end' ];
	f.set ( dest_div );
};
//}}}
//{{{user.modify_exit
user.modify_exit = function ()
{
	$( user._panel_id , String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], user.__info ) );
	return;	
}
//}}}
//{{{user.modify = function( exit )
user.modify = function( evt )
{
	//var frm = liwe.form.get ( "user-profile-form" );
	var form_id = 'user-profile-form';
	var frm = liwe.form5.get ( form_id );
	if ( ! frm.check () ) return;

	var post = frm.get_values ();


	if ( post [ 'pwd' ] != post [ 'pwd2' ] )
	{
		$ ( 'err_descr', 'Le nuove password non coincidono' )
		return false;
	}


	post [ 'action' ] = 'user.ajax.modify';
	user.ajax ( post , function ( resp )
	{
		user.__info = resp;
		// parse form and prepare extra fields list
		user.dispatchEvent ( "modify", null, resp );
		var data_list = user._parse_form_user_data ( form_id, post );
		if ( data_list ) user.user_data_multiset ( data_list, resp );
		$ ( 'err_descr', 'Dati salvati correttamente' )

	},function ( txt, err_descr, err_code, data )
	{
		$ ( 'err_descr', err_descr )
		return false;	
	} );
	return false;

}
//}}}
//{{{user._parse_form_user_data
user._parse_form_user_data = function ( form_id, form_fields )
{
	if ( ! form_fields )
	{
		var f = liwe.form5.get ( form_id );
		form_fields = f.get_values ();
	}
	var match = 'user_data_field';
	var data_list = list ();
	form_fields.iterate ( function ( val, field )
	{
		if ( field.indexOf ( match ) == -1 ) return false;
		var tmp = $( form_id + '-' + field );
		if ( ! tmp ) return;
		var data = tmp.dataset;
		data_list.push ( { 	
			'id_data_type': data [ 'idtype' ],
			'id_user_data': data [ 'iduser' ],
			'value': val
			} );
	} ); 
	return data_list;
};
//}}}
//{{{user.add_data_fields
user.add_data_fields = function ( f, user_data, data )
{
	function _alt_val ( idx, field )
	{
		return data.get ( 'user_data_field_' + idx );
	} 
	var tpl = '';
	user_data = user_data || list ();
	user_data.iterate ( function ( d, idx )
	{
		var f_name = 'user_data_field_' + idx;
		var dct = { 	'label' : d.get ( 'name', '' ), 
				'value' : d.get ( 'data', _alt_val ( idx ) ), 
				'data-idType' : d.get ( 'id_data_type', '' ), 
				'data-idUser' : d.get ( 'id_user_data', 0 ),
				'nonl' : false
			} ;
		f.add ( "input", f_name, dct );
		var tpl_vals = { 	'label' : '%(' + f_name + '-label)s',
					'value' : '%(' + f_name + ')s'
				};
		tpl += String.formatDict ( user.templates [ 'frm-user-modify-extra' ], tpl_vals ); 
	} );
	return tpl;
};
//}}}
//{{{user.user_data_type_list
user.user_data_type_list = function ( cback )
{
	user.ajax ( { 'action' : 'user.ajax.user_data_type_list' }, function ( data )
	{
		user.data_type_list = dict ();
		user.data_type_opts = list ();
		user.data_type_opts.push ( { 'value' : 0, 'label' : '' } );
		data.get ( 'data_type' ).iterate ( function ( t ) 
		{ 
			user.data_type_list [ t.id_data_type ] = t 
			user.data_type_opts.push ( { 'value' : t.id_data_type, 'label' : t.name } );
		} );
		user.dispatchEvent ( 'data-type-list', null, { 'type_list' : user.data_type_list } );
		if ( cback ) cback ();
	} );
};
//}}}
//{{{user.user_data_type_get
user.user_data_type_get = function ( id_data_type, name, cback )
{
	var vals = {	'action'	: 'user.ajax.user_data_type_list',
			'id_data_type'	: id_data_type,
			'name'		: name
			};
	user.ajax ( vals,  function ( data ) 
	{ 
		var data_type = data.get ( 'data_type' );
		user.dispatchEvent ( 'data-type-get', null, { 'data_type' : data_type } );
		if ( cback ) cback ( data_type ); 
	} );
};
//}}}
//{{{user.user_data_get
user.user_data_get = function ( id_data_type, cback )
{
	var vals = {	'action'	: 'user.ajax.user_data_get',
			'id_data_type'	: id_data_type
			};
	user.ajax ( vals,  function ( data ) 
	{ 
		var user_data = data.get ( 'user_data' );
		user.dispatchEvent ( 'user-data-get', null, { 'user_data' : user_data } );
		if ( cback ) cback ( user_data ); 
	} );
};
//}}}
//{{{user.user_data_set
user.user_data_set = function ( id_data_type, value, id_user_data )
{
	var vals = {
		'action'	: 'user.ajax.user_data_set',
		'id_data_type'	: id_data_type,
		'value'		: value,
		'id_user_data'	: id_user_data
		};
	user.ajax ( vals, function ( data )
	{
		delete vals.action;
		vals [ 'id_user_data' ] = data.get ( 'id_user_data' )
		user.dispatchEvent ( 'user-data-set', null, vals );
	} );
};
//}}}
//{{{UPDATE/DELETE multiple user_data fields
/*
	- data_list: [	{
			'id_data_type':	data type ( optional when delete ) 
			'id_user_data': id user data ( needed for update, optional: delete/insert )
			'value': 	user data value ( if empty the field will be deleted )
			}	]
*/
user.user_data_multiset = function ( data_list, resp )
{
	if ( user.user_data_busy ) { alert ( 'Aggiornamento in corso. Attendere prego.' ); return };
	data_list.iterate ( function ( val )
	{
		var vals = {};
		var value = val.get ( 'value' );
		vals = { 
			'action'	: 'user.ajax.user_data_set',
			'id_data_type'	: val.get ( 'id_data_type' ),
			'id_user_data'	: val.get ( 'id_user_data' ),
			'value'		: val.get ( 'value' )
		};
		
		liwe.AJAX.add ( 'user_data', liwe.AJAX.url, vals );
	} );
	user.dispatchEvent ( 'user-data-updating', null, { 'data_list' : data_list } );
	liwe.AJAX.start ( 'user_data', function () { user.dispatchEvent ( 'user-data-multiset', null, { 'data_list' : data_list, 'modify-resp' : resp  } ); } ); 
};
//}}}
//{{{USER 2
//{{{user.key_down = function ( evt, el )
/*
user.key_down = function ( evt, el )
{
	evt = evt || window.event;

	if ( evt.which == 13 || evt.keyCode == 13 )
	{
		if ( el.name == "login" )
		{
			var f  = liwe.form.get ( "user-login-form" );
			var pwd = f.get_element ( 'pwd' );
			pwd.focus();
			return
		}
		user.login ();
	}
};
//}}}
//{{{user.login = function ()
user.login = function ()
{
	// var f = liwe.form.get ( "user-login-form" );
	var f = liwe.form5.get ( "user-login-form" );
	if ( ! f.check () ) return;
	var a = f.get_values ();
	var cback;

	a [ 'action' ] = "user.ajax.login";
	user.ajax ( a, function ( v ) {
		v [ 'module' ] = a [ 'module' ];
		user._uid = v [ 'uid' ];
		user.dispatchEvent ( "stat-login", null, v );
		user.dispatchEvent ( "login", null, v );
	} );
};
//}}}
//{{{user.register = function ()
user.register = function ()
{
	var frm = liwe.form.get ( "user-login-register" );
	if ( ! frm.check () ) return;
	var vals = frm.get_values ();

	if ( vals [ 'pwd' ] != vals [ 'pwd2' ] )
	{
		var dct = { 'alert' : user.templates [ 'SITE_REG_MSG_PWD' ], 'module' : vals [ 'module' ] } 
		$( 'alert-registration' , dct [ 'alert' ] );	
		user.dispatchEvent ( "register_check", null, dct );
		return;
	}

	user.ajax ( { action: "user.ajax.login_exists", email: vals [ 'email' ] }, function ( v )
	{
		v [ 'module' ] = vals [ 'module' ];
		if ( v [ 'exists' ] )
		{
			v [ 'alert' ] = user.templates [ 'SITE_REG_MSG_MAIL' ]
			$( 'alert-registration' , v [ 'alert' ] );	
			user.dispatchEvent ( "register_check", null, v );
			return;
		}

		if ( ! vals [ 'login' ] ) vals [ 'login' ] = vals [ 'email' ];

		vals [ 'action' ] = 'user.ajax.register';
		user.ajax ( vals, function ( v ) 
		{
			v [ 'email' ] = vals [ 'email' ];
			v [ 'module' ] = vals [ 'module' ];
			user.dispatchEvent ( "register", null, v );
		} );
	} );
};
//}}}
//{{{user.lost_password = function ()
user.lost_password = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var email = f.get_value ( "email" );

	if ( ! email ) return;

	user.ajax ( { action: "user.ajax.lost_password", email: email }, function ( v )
	{
		var res = { 'alert' : String.formatDict ( user.templates [ 'SITE_LOST_PWD' ] , { 'email' : email } ) }; 
		user.dispatchEvent ( "lostpwd", null, res );
	}, function ( txt, err_text, err_code, res )
	{
		var res = { 'alert' : err_text }; 
		user.dispatchEvent ( "lostpwd", null, res );
	} );
};
//}}}
//}}}
//{{{OLD FUNCTION FOR COMPATIBILITY
user.login_box = function ()
{
	var frm = user.login_form();
	//liwe.lightbox.easy ( "user-lightbox", "Accesso utente", 450, 300, -1, 100 );
	liwe.lightbox.easy ( "user-lightbox", "Accesso utente", 343, 350 );

	frm.set ( "user-lightbox" );

};

user.login_form = function ( cback )
{
	var frm =  liwe.form.get ( "user-login-form" );

	if ( frm ) 
	{
		frm._login_cback = cback;
		return frm;
	}

	frm = new liwe.form.instance ( "user-login-form" );

	frm._login_cback = cback;

	frm.text ( { label: "Login / Email", name: "login", size: 20, maxlength: 50, mandatory: true, onkeydown: "user._key_down(event)" } );
	frm.password ( { label: "Password", name: "pwd", size: 20, maxlength: 30, mandatory: true, onkeydown: "user._key_down(event)" } );
	frm.button ( { value: "Accedi", onclick: "user._login()", 'class': 'green' } );
	frm.sep ();
	frm.descr ( "Hai dimenticato la password?" );
	frm.text ( { label: "Tua e-mail", name: "email", size: 20, maxlength: 50 } );
	frm.button ( { value: "Richiedi", onclick: "user._lost_password()", 'class': 'blue' } );
	frm.sep ();
	frm.descr ( "Registrazione nuovo utente" );
	frm.label ( { value: "&nbsp;<br />La registrazione a questo sito &egrave; totalmente gratuita!<br />&nbsp;<br />" } );
	frm.button ( { value: "Vuoi registrarti al sito? Clicca qui!", onclick: "user.registration()", 'class': 'yellow' } );
	if ( typeof ( social ) != 'undefined' )
	{
		social.facebook.cbacks [ 'login' ] = user._facebook_login;
		social.facebook.cbacks [ 'init' ] = user._facebook_init;
		frm.label ( { value: social.facebook.get_login () } );
	}
	return frm;
};

user.registration_form = function ( clear )
{
	var frm =  liwe.form.get ( "user-login-register" );
	if ( typeof ( frm ) != 'undefined' )
	{
		if ( clear ) frm.clear ();
		return frm;
	};

	frm = new liwe.form.instance ( "user-login-register" );
	
	frm.email ( { label: "Indirizzo e-mail", name: "email", size: 30, maxlength: 200, mandatory: true } );
	frm.password ( { label: "Password", name: "pwd", size: 30, maxlength: 30, mandatory: true } );
	frm.password ( { label: "Password (conferma)", name: "pwd2", size: 30, maxlength: 30, mandatory: true } );
	frm.button ( { value: "Registrati", onclick: "user._register()" } );
	
	return frm;
	
};

user.registration = function ()
{
	var frm = user.registration_form();

	frm.set ( "user-lightbox" );

};

user._register = function ()
{
	var frm = liwe.form.get ( "user-login-register" );
	var vals = frm.get_values ();

	if ( vals [ 'pwd' ] != vals [ 'pwd2' ] )
	{
		alert ( "Verifcare la password inserita" );
		return;
	}

	user.ajax ( { action: "user.ajax.login_exists", email: vals [ 'email' ] }, function ( v )
	{
		if ( v [ 'exists' ] )
		{
			alert ( "L'indirizzo di email inserito e' gia' presente nel nostro database." );
			return;
		}

		if ( ! vals [ 'login' ] ) vals [ 'login' ] = vals [ 'email' ];

		vals [ 'action' ] = 'user.ajax.register';
		user.ajax ( vals, function ( v ) 
		{
			v [ 'email' ] = vals [ 'email' ];
			user.events [ 'registered' ] && user.events [ 'registered' ] ( v );
			liwe.lightbox.close ();
		} );
	} );
};

user._change_password = function ( data )
{
	if ( ! data || typeof ( data ) != 'object' || ! data [ 'token' ] ) return;
	user.change_password ( data [ 'email' ], data [ 'token' ] );
};

user.change_password = function ( email, token )
{
	var frm = new liwe.form.instance ( "user-change-pwd-form" );
	frm.password ( { label: "Password", name: "pwd", size: 12, maxlength: 30, mandatory: true } );
	frm.password ( { label: "Password (verifica)", name: "pwd2", size: 12, maxlength: 30, mandatory: true } );
	frm.descr ( '' );
	frm.descr ( 'Inserisci qui la nuova password per il tuo account.' );
	frm.descr ( 'Riceverai una mail con i nuovi dati per accedere al sito' );
	frm.descr ( '' );
	frm.hidden ( { name: "email", value: email } );
	frm.hidden ( { name: "token", value: token } );
	frm.button ( { value: "Salva", onclick: "user._change_pwd()" } );

	liwe.lightbox.easy ( "user-lightbox", "Modifica password", 300, 80 );

	frm.set ( "user-lightbox" );
};

user._change_pwd = function ()
{
	var f = liwe.form.get ( "user-change-pwd-form" );
	var a;

	if ( ! f.check () ) return;

	a = f.get_values ();

	if ( a [ 'pwd' ] != a [ 'pwd2' ] ) 
	{
		alert ( "Le due password inserite non coincidono" );
		return;
	}

	user.ajax ( { action: "user.ajax.change_password", pwd: a [ 'pwd' ], token: a [ 'token' ], email : a [ 'email' ] }, function ( v )
		{
			alert ( "Password aggiornata!" );
			liwe.lightbox.close ();
		} );

};


user._login = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var a = f.get_values ();
	var cback;

	a [ 'action' ] = "user.ajax.login";

	user.ajax ( a, function ( v ) {
		if  ( liwe.lightbox.created() )	liwe.lightbox.close ();
		user._uid = v [ 'uid' ];

		cback = f._login_cback;
		f._login_cback = null;

		if ( cback )
		{
			cback ( v );
		} else {
			if ( user.events.login ) user.events.login ( v );
		}
	} );
};

user._lost_password = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var email = f.get_value ( "email" );

	if ( ! email ) return;

	user.ajax ( { action: "user.ajax.lost_password", email: email }, function ( v )
		{
			alert ( "Una nuova password e' stata inviata all'indirizzo di email: " + email );
		} );
};

user._key_down = function ( e )
{
	e = e || window.event;

	if ( e.which == 13 || e.keyCode == 13 )
		user._login ();
};

user._facebook_login = function ( res )
{
	social.facebook.user_login ( function ( res )
	{
		if ( res.text != 'OK' )
		{
			alert ( res.text );
		}
		window.location.reload ();
	} );
};

user._facebook_init = function ()
{
	if ( social.facebook.is_logged () )
	{
		var data = social.facebook.get_user ();
		$ ( 'social-facebook-text', 'Login come ' + data.name );
	}
};
//}}}
*/
